#!/bin/bash
########################################################################
# script name:  remove_user_custom_settings.sh
# Used for:
# Author:       Martin Dumont
# Contributors:
# Version:      0.1
#
# (date format: date +'%Y/%m/%d')
#
# Date          Version Contributors    Modifications
# ----------    ------- ------------    -------------
# 2012/06/04    0.1     MDUMONT         initial
########################################################################
usage()
{
cat << STOP

usage: $0 [options] [ARG]

This script is used for ....

OPTIONS:
   -u               User

Ex.:
$0 -u b0528817
STOP
}
#######################################
# Get parameters
#######################################
while getopts u: OPTION
do
        case $OPTION in
        u)
                TGTUSER=$(echo $OPTARG | tr '[A-Z]' '[a-z]')
                ;;
        \?)
                usage
                exit 1
                ;;
        *)
                usage
                exit 1
                ;;
        esac
done
if [ $OPTIND = 1 ]; then usage; exit 1; fi
#######################################
# Variables and environnement
#######################################
BAENV=$(dirname `which $0`)/loadenv.sh
if [ -f $BAENV ]; then . $BAENV; else echo "No env library ($BAENV)"; exit 1; fi
#######################################
# MAIN
#######################################
if cat /etc/passwd | cut -d: -f1 | grep -wq $TGTUSER
then
	bk /etc/passwd
	/bin/echo "Removing custom entries for user $TGTUSER: \c"
	if cp -rp /etc/passwd /tmp/passwd_temp && rmuser -p $TGTUSER && cp -rp /tmp/passwd_temp /etc/passwd
	then
		echo ok
	fi
	/bin/echo "Verifying user $TGTUSER kerberos settings: \c"
	if lsuser -a SYSTEM $TGTUSER | grep -q KRB5A
	then
		echo "ok"
		/bin/echo "Clearing password flags: \c"
		if /bin/pwdadm -c $TGTUSER
		then
			/bin/echo ok
		else
			/bin/echo "Couldn't clear the password flag (/bin/pwdadm -c $TGTUSER), please verify"
			exit 1
		fi
	else
		echo "Not good, verify with \"lsuser -a SYSTEM $TGTUSER\""
		exit 1
	fi
else
	echo "User $TGTUSER doesn't exist"
	exit 1
fi
#######################################
#echo
#echo "Listing non-Kerberos users:"
#lsuser -a SYSTEM ALL | grep -v KRB5A | awk '{print "   "$1}'
#echo
#echo "Execute the corresponding command to change the user that needs to be on Kerberos :"
#for i in `lsuser -a SYSTEM ALL | grep -v KRB5A | grep -v root | awk '{print $1}'`
#do
#   echo "   cp -rp /etc/passwd /tmp/passwd_temp && rmuser -p $i && cp -rp /tmp/passwd_temp /etc/passwd"
#done
#echo
#echo "A log has been created: $LOG"
#echo
#######################################
exit 0

